<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class AttachmentsController extends AppController {

/**
 * Define $name
 *
 */
    var $name = 'Attachments';
/**
 * Define $helpers
 *
 */
    var $helpers = array('Html', 'Form','Text');
/**
 * Define $uses
 *
 */
    var $uses = array('Attachment','Committee');

/**
 * Describe index
 *
 * @param $committee
 * @return null
 */
    function index($committee) {
        
        //optimization
		if (isset($this->params['pass'][1])) {
			$this->data['Search']['item'] = $this->params['pass'][1];
		}

        $this->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Userstatus','Groupstatus','Wfmodel','Todo','Decision'),'hasAndBelongsToMany'=>array('User')),false);
        $this->Committee->Item->unbindModel(array('hasMany'=>array('Decision')),false);
        $this->Committee->Meeting->Behaviors->detach('Comment');
        $this->Committee->Meeting->Behaviors->detach('MultiFile');
        $this->Committee->Meeting->unbindModel(array('hasMany'=>array('Decision','Todo','Notification'),'hasAndBelongsToMany'=>array('User','Membership')),false); 
        $this->Committee->Meeting->Decision->Behaviors->detach('Comment');
        $this->Committee->Meeting->Decision->Behaviors->detach('MultiFile');
        $this->Committee->Meeting->Decision->unbindModel(array('hasMany'=>array('Userstatus','Groupstatus'),'hasAndBelongsToMany'=>array('User','Group')),false);

        $projects = $this->Committee->Item->find(
            'list',array(
                'conditions'=>array(
                    'Item.committee_id'=>$this->dcommittee['Committee']['id'],
                ),
                'order'=>'Item.name ASC',
            )
        );
        $meetings=$this->Committee->Meeting->find(
            'list',array(
                'conditions'=>array(
                     'Meeting.committee_id'=>$this->dcommittee['Committee']['id'],
                     'Meeting.deleted=0',
                 )
            )
        );
        $this->set(compact('projects','meetings'));
        
        if(!empty($this->data['Search']['name']) || !empty($this->data['Search']['type'])||!empty($this->data['Search']['meeting'])|| !empty($this->data['Search']['item']) ||!empty($this->data['Search']['year'])){
            $not=$this->Committee->deletedDecisionId();
            $file_id=$this->Attachment->CommitteeFileId($this->dcommittee['Committee']['id'],$not);
            
            //find the attachment id if the filename or type or year are input
            if (!empty($this->data['Search']['name']) || !empty($this->data['Search']['type'])|| !empty($this->data['Search']['year'])){
                $cond_id['Attachment.id']=$file_id;
                if(!empty($this->data['Search']['name'])){
                    $cond_id['Attachment.filename LIKE']='%'.$this->data['Search']['name'].'%';
                }
                if(!empty($this->data['Search']['type'])) {
                    $cond_id['Attachment.model']=$this->data['Search']['type'];
                }
                if(!empty($this->data['Search']['year'])){
                    $cond_id['Attachment.modified >=']=date("Y-m-d H:i:s",mktime(0,0,0,1,1,$this->data['Search']['year']));
                    $cond_id['Attachment.modified <=']=date("Y-m-d H:i:s",mktime(23,59,59,12,31,$this->data['Search']['year']));
                }
                $fileids=Set::extract($this->Attachment->find('all',array('conditions'=>$cond_id)),'{n}.Attachment.id');    
            }
        
            //conditions for attachment id 
            if(isset($fileids)){
                $cond1['Attachment.id']=$fileids;
            }else{
                $cond1['Attachment.id']=$file_id;
            }
            
            $cond=array();
            //once the meeting are selected
            if(!empty($this->data['Search']['meeting'])){
                $cond['meeting_id']=$this->data['Search']['meeting'];
                $cond_meeting['foreign_key']=$this->data['Search']['meeting'];
            }
            
            //once the item are selected
            if(!empty($this->data['Search']['item'])){
                $cond['Decision.item_id']=$this->data['Search']['item'];
            }else{
                //there will be meetingfile only as the item is not selected
                $cond_meeting['Attachment.model']='Meeting';
                $cond_meeting=array_merge($cond_meeting,$cond1);
                $meetingfiles=$this->Attachment->meetingFiles($cond_meeting);
            }
            
            $cond['not']['Decision.id']=$not;
            $cond_decision['Decision.committee_id']=$this->dcommittee['Committee']['id'];
            //find userstatusfiles
            $userstatusfile=$this->Attachment->userstatusFiles(array('Attachment.id'=>$cond1['Attachment.id'],'Attachment.model'=>'Userstatus'));
            foreach($userstatusfile as $u){
                $cond_decision['Decision.id']=$u['Userstatus']['decision_id'];
                if($this->Committee->Meeting->Decision->find('count',array('conditions'=>array_merge($cond,$cond_decision)))){
                    $u['Attachment']['viewpermission']=$this->Attachment->viewPermission($u,$this->Auth->User('id'));
                    $userstatusfiles[]=$u;
                }
            }
            //find groupstatusfiles
            $groupstatusfile=$this->Attachment->groupstatusFiles(array('Attachment.id'=>$cond1['Attachment.id'],'Attachment.model'=>'Groupstatus'));
            foreach($groupstatusfile as $g){
                $cond_decision['Decision.id']=$g['Groupstatus']['decision_id'];
                if($this->Committee->Meeting->Decision->find('count',array('conditions'=>array_merge($cond,$cond_decision)))){
                    $g['Attachment']['viewpermission']=$this->Attachment->viewPermission($g,$this->Auth->User('id'));
                    $groupstatusfiles[]=$g;
                }
            }
            //find decisionfiles
            $cond=array_merge($cond,$cond1);
            $decisionfiles=$this->Attachment->decisionFiles($cond);
    
            if(isset($meetingfiles) && !empty($meetingfiles)) {
                foreach($meetingfiles as $mkey=>$m){
                    $meetingfiles[$mkey]['Attachment']['viewpermission']=1;
                }
                $this->set('meetingfiles',$meetingfiles);
            }
            if(isset($decisionfiles)) {
                foreach($decisionfiles as $dkey=>$d){
                    $decisionfiles[$dkey]['Attachment']['viewpermission']=$this->Attachment->viewPermission($d,$this->Auth->User('id'));
                }
                $this->set('decisionfiles',$decisionfiles);
            }

            if(isset($userstatusfiles)) $this->set('userstatusfiles',$userstatusfiles);
            if(isset($groupstatusfiles)) $this->set('groupstatusfiles',$groupstatusfiles);

        }
        $year=date('Y');
        $years[$year]=$year;
        for($i=1;$i<=5;$i++){
            $years[$year-$i]=$year-$i;
            $years[$year+$i]=$year+$i;
        }
        asort($years);
        $this->set('years',$years);
    }
}
?>
